/*

    This file is part of Hometree.

    Hometree is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Hometree is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Hometree.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2011,2012 Roman Held

*/

/********************************************************************\
                          Includes
\********************************************************************/
#include "htrsmap.h"
#include "htrsmimedataresource.h"
#include "htrsplugins.h"
#include "htr/htrglobal.h"
#include "htr/htrpersistentservice.h"

/********************************************************************\
                          Implementation
\********************************************************************/

/*--------------------------------------------------------------------
*/
HtrsMap::HtrsMap()
 : QObject()
{
  mCurrentChangesPerSecond = 0;
  mLastChangesPerSecond    = 0;
  connect(&mChangesPerSecondTimer, SIGNAL(timeout()),
          this, SLOT(changesPerSecondStatistics()));
  mChangesPerSecondTimer.start(1000);

  // TODO: restore:
  if (HtrsPlugins::instance()->persistentService()) {
    QList<QObject*> list = HtrsPlugins::instance()->persistentService()->restoreAll();
    foreach(QObject *obj, list) {
      HtrsResource *r = dynamic_cast<HtrsResource*>(obj);
      if (r) { // ! ? wtf..?
        connect(r, SIGNAL(changed()), 
            this, SLOT(senderChanged()));
        connect(r, SIGNAL(aboutToDelete()), 
            this, SLOT(senderAboutToDelete()));
        mResources.insert(r->url(), r);
      }
    } 
  }
}

/*--------------------------------------------------------------------
*/
HtrsMap::~HtrsMap()
{
}

/*--------------------------------------------------------------------
*/
HtrsResource *HtrsMap::access(const QString &ident,  Htr::ResourceType type, bool &created)
{
  HtrsResource *&r = mResources[ident];
  
  if (!r) {
    r = create(ident,type);
    created = true;
  } else {
    created = false;
  }
 
  return r;
}

/*--------------------------------------------------------------------
*/
QList<HtrsResource*> HtrsMap::resources() const
{
  return mResources.values();
}

/*--------------------------------------------------------------------
*/
void HtrsMap::senderChanged()
{
  HtrsResource *r = dynamic_cast<HtrsResource*>(sender());
  Q_ASSERT(r);
  mCurrentChangesPerSecond++;  // fuer Statistik-Zwecke..
  emit changed(r);
}

/*--------------------------------------------------------------------
*/
void HtrsMap::senderAboutToDelete()
{
  HtrsResource *r = dynamic_cast<HtrsResource*>(sender());
  Q_ASSERT(r);
  emit aboutToDelete(r);
  mResources.remove(r->url()); 
}

/*--------------------------------------------------------------------
*/
void HtrsMap::changesPerSecondStatistics()
{
  if (mCurrentChangesPerSecond != mLastChangesPerSecond) {
    mLastChangesPerSecond    = mCurrentChangesPerSecond;
    emit changesPerSecond(mLastChangesPerSecond);
  }
  mCurrentChangesPerSecond = 0;
}

/*--------------------------------------------------------------------
*/
HtrsResource *HtrsMap::create(const QString &ident, Htr::ResourceType type)
{
  // TODO: Factories:
  Q_ASSERT(type == Htr::MimeDataType); 

  HtrsResource *r = new HtrsMimeDataResource(ident);
  r->setObjectName(ident.toAscii().toBase64()); // unique ident

  connect(r, SIGNAL(changed()), 
          this, SLOT(senderChanged()));
  connect(r, SIGNAL(aboutToDelete()), 
          this, SLOT(senderAboutToDelete()));

  emit created(r);
  return r;
}

